package com.nasarius.bank.scorecard.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.csvreader.CsvReader;
import com.nasarius.bank.scorecard.domain.BankFactorsActual;

public class CSVUtil {
	private static final SimpleDateFormat FORMATTER=  new SimpleDateFormat("dd-MM-yyyy");
	public static void main(String[] args) {

	}

	public static ArrayList<BankFactorsActual> readCSV(InputStream is) throws Exception{
		ArrayList<BankFactorsActual> bankFactorsActualList = new ArrayList<BankFactorsActual>();
		try {

			CsvReader factorsActuals = new CsvReader(new InputStreamReader(is));
			
			factorsActuals.readHeaders();

			while (factorsActuals.readRecord()) {
				BankFactorsActual bankFactorsActual= new BankFactorsActual();
				bankFactorsActual.setBankcode(factorsActuals.get("BANKCODE"));
				bankFactorsActual.setFactorName(factorsActuals.get("FACTORNAME"));
				bankFactorsActual.setFromdt(FORMATTER.parse(factorsActuals.get("STARTDATE")));
				bankFactorsActual.setTodt(FORMATTER.parse(factorsActuals.get("ENDDATE")));
				bankFactorsActual.setActualvalue(Integer.valueOf(factorsActuals.get("ACTUALVALUE")));
				bankFactorsActualList.add(bankFactorsActual);
			}
			
			factorsActuals.close();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} catch (ParseException e) {
			e.printStackTrace();
			throw e;
		}
		return bankFactorsActualList;
	}

}
